//
//  RidingAssistanceViewController.m
//  INMOTION
//
//  Created by cjg on 2020/4/16.
//  Copyright © 2020 Inmotion. All rights reserved.
//

#import "RidingAssistanceViewController.h"
#import "KSBLEManager.h"
#import "IMTConfig.h"
#import "IMTCANManager.h"
#import "KSLanguageManager.h"
#import "NSObject+HUD.h"

@interface RidingAssistanceViewController ()
@property (weak, nonatomic) IBOutlet UILabel *forceTitleLB;
@property (weak, nonatomic) IBOutlet UISwitch *forceSwitch;
@property (weak, nonatomic) IBOutlet UILabel *uphillTitleLB;
@property (weak, nonatomic) IBOutlet UISwitch *uphillSwitch;
@property (weak, nonatomic) IBOutlet UILabel *downhillTitleLB;
@property (weak, nonatomic) IBOutlet UISwitch *downhillSwitch;

@end

@implementation RidingAssistanceViewController
- (void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = IMTNewCarLocalizedString(@"car_feature_riding_assist");
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back_white"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = left;

    self.downhillTitleLB.text = IMTNewCarLocalizedString(@"car_feature_hdc");
    
    self.tableView.scrollEnabled = NO;
    self.forceSwitch.onTintColor = kIMTDefaultOrangeColor;
    self.downhillSwitch.onTintColor = kIMTDefaultOrangeColor;
    self.uphillSwitch.onTintColor = kIMTDefaultOrangeColor;
    BOOL enabled = [KSBLEManager sharedManager].state == KSBLEManagerStateSubscribed;
    self.tableView.allowsSelection = enabled;
    [self.tableView reloadData];
    
    if([KSBLEManager sharedManager].state == KSBLEManagerStateSubscribed) {
        [[IMTCANManager sharedInstance] clearMessages];
        // 读取设置项状态
        [self.l9Carmodel getDataWithCmdType:(L9_CarModelCmdTypeComplexSetting) isRepeat:YES compleBlock:^(NSError *error, NSData *responseData) {
            NSLog(@"获取所有设置项状态的结果 ================= ⬆️🐍 %@ -- %@ -- %d",error,responseData,self.l9Carmodel.lockSwitch);
            [self.tableView reloadData];
        }];
    }
}

#pragma mark - setting
- (IBAction)downhillSwitch:(UISwitch *)sender {
    [self showProgressHUDWithText:nil];
    WS(weakSelf)
    uint8_t value = (uint8_t)sender.on;
    [self.l9Carmodel writeDataWithCmdType:(L9_CarModelCmdTypeDownhillAssist) uint8Param:value successBlock:^{
        L9_SettingParams param = self.l9Carmodel.settingParams;
        param.downhill_assist = value;
        self.l9Carmodel.settingParams = param;
        self.l9Carmodel.downhillAssistSwitch = value == 1;
        [weakSelf showSuccessHUDWithText:IMTNewCarLocalizedString(@"setting_success")];
        [weakSelf.tableView reloadData];
    } failBlock:^(NSError *error) {
        [weakSelf showErrorHUDWithText:error.userInfo[@"description"]];
        [weakSelf.tableView reloadData];
    }];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    self.downhillSwitch.on = self.l9Carmodel.downhillAssistSwitch;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return nil;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    return IMTNewCarLocalizedString(@"car_feature_hdc_desc");
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return [super tableView:tableView heightForFooterInSection:section];
}

#pragma mark - TableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

@end
